import util from '../../utils/util.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    islogin: false,
    user: null,
    lover: null,
    ishave: true, // 是否有纪念日
    modalName: null,
    date: '',
    time: '',
    Time: ''
  },
  getEvent(_openid) {
    const db = wx.cloud.database()
    const _ = db.command
    return db.collection('event').where(_.or([
      {_openid: _openid},
      {lover_openid: _openid}
      ])).get()
  },
  showModal() {
    var date = new Date
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    this.setData({
      date: year + '/' + month + '/' + day,  // 日期选择默认当前日期
      modalName: 'addModal'
    })
  },
  resetModal(){
    this.setData({
      date: this.data.event.time,  // 日期
      modalName: 'resetModal'
    })
  },
  hideModal() {
    this.setData({
      modalName: null
    })
  },
  DateChange(e) {
    var arr = e.detail.value.split("-");
    this.setData({
      date: arr[0] + '/' + arr[1] + '/' + arr[2],
    });
  },
  addEvent() {
    wx.showLoading({
      title: '正在添加'
    })
    wx.cloud.database().collection('event').add({
      data: {
        lover_openid: this.data.lover._openid,
        time: this.data.date
      }
    }).then(res => {
      wx.hideLoading()
      wx.showToast({
        title: '添加成功',
      })
      this.setData({
        modalName: null,
      })
      this.geton()
    })
  },
  resetEvent(){
    wx.showLoading({
      title: "修改中"
    })
    this.data.event.time = this.data.date
    wx.cloud.callFunction({
      name: 'updateEvent',
      data:{
        e: this.data.event
      }
    }).then(res=>{
      wx.hideLoading()
      wx.showToast({
        title: '修改成功',
      })
      this.setData({
        event: this.data.event,
        modalName: null
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  
   
  },
  geton(){ // 获得 时间并且倒计时
    var that = this;
    var user = wx.getStorageSync('user')
    this.getEvent(user._openid).then(res => {
      this.setData({
        ishave: res.data.length == 0 ? false : true,
        event: res.data.length == 0 ? '' : res.data[0]
      })
      if (this.data.ishave) {
        setInterval(function () {
          var time = that.timedifference(that.data.event.time, util.formatTime2(new Date))
          that.setData({
            Time: time
          });
        }, 1000);
      }
    })
  },
  timedifference: function (faultDate, completeTime) {
    var stime = Date.parse(new Date(faultDate));//获得开始时间的毫秒数
    var etime = Date.parse(new Date(completeTime));//获得结束时间的毫秒数
    var usedTime = etime - stime; //两个时间戳相差的毫秒数
    var days = Math.floor(usedTime / (24 * 3600 * 1000));
    //计算出小时数
    var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000));//将剩余的毫秒数转化成小时数
    //计算相差分钟数
    var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
    var minutes = Math.floor(leave2 / (60 * 1000));//将剩余的毫秒数转化成分钟
    //计算相差秒数
    var leave3 = leave2 % (60 * 1000);//计算分钟数后剩余的毫秒数
    var seconds = Math.floor(leave3 / 1000);//将剩余的毫秒数转化成秒数

    var dayStr = days == 0 ? "" : days + "天";
    var hoursStr = hours == 0 ? "" : hours + "小时";
    var minutesStr = minutes == 0 ? "" : minutes + "分"
    var time = dayStr + hoursStr + minutesStr + seconds + "秒";
    return time;
  },//计算两个时间之间的时间差
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var user = wx.getStorageSync('user')
    var lover = wx.getStorageSync('lover')
    this.setData({
      islogin: app.globalData.islogin,
      user: user,
      lover: lover,
    })
    if(this.data.islogin){
      this.geton()
    }
  },
  gologin(){
    wx.reLaunch({
      url: '/pages/me/me',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})